package com.cl.springbootfiles.vo;


import lombok.Builder;
import lombok.Data;
import java.text.DecimalFormat;
import java.util.Date;

@Data
@Builder
public class FileVO {
    private String fileName;
    private String fileSize;
    private Date lastModified;
    private static final long LEN_B = 1024L;
    private static final long LEN_K = 1048576L;
    private static final long LEN_M = 1073741824L;
    private static final long LEN_G = 1099511627776L;

    public static String formatFileSize(long length) {
        assert length > 0L : "参数有误，Length必须大于0";

        DecimalFormat df = new DecimalFormat("#.00");
        if (length < LEN_B) {
            return df.format((double) length) + "B";
        } else if (length < LEN_K) {
            return df.format((double) length / (double) LEN_B) + "K";
        } else {
            return length < LEN_M ? df.format((double) length / (double) LEN_K) + "M" : df.format((double) length / (double) LEN_M) + "G";
        }
    }
}